package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class DrawCurve extends Sprite implements IGetRoadArr
	{
//----------------------------------------------------------------------------------------------------PUBLIC---------------------------------
													//================ Properties ===============
										
													//================ Public Methods ===========
		public function DrawCurve(spr:Sprite):void{
			container=spr;
			initSpr();
			initVar();
			initListener();
		}
		public function getRoadArr():Array{
			getPtList();
			return ptList;
		}
		public function draw():void {
			if(anList.length>1){
				var g:Graphics = this.graphics;
				g.clear();
				var prevMidpt:Point = null;
				var l:Number = anList.length;
				for (var i:Number=1;i<l;i++) {
					var pt1:Object = anList[i-1];
					var pt2:Object = anList[i];				
					g.lineStyle(0,0xBBBBBB,0.6);
					g.moveTo(pt1.x,pt1.y);
					g.lineTo(pt2.x,pt2.y);				
					g.lineStyle(0,0xBBBBBB,0.5);
					var midpt:Point = new Point(pt1.x+(pt2.x-pt1.x)/2,pt1.y+(pt2.y-pt1.y)/2);
					var a:Number = Math.atan2(pt2.y-pt1.y,pt2.x-pt1.x);
					g.moveTo(midpt.x+Math.cos(a+Math.PI/2)*8,midpt.y+Math.sin(a+Math.PI/2)*8);
					g.lineTo(midpt.x-Math.cos(a+Math.PI/2)*8,midpt.y-Math.sin(a+Math.PI/2)*8);				
					g.lineStyle(2,_color,1);
					if (prevMidpt) {
						g.moveTo(prevMidpt.x,prevMidpt.y);
						g.curveTo(pt1.x,pt1.y,midpt.x,midpt.y);
					} else {
						g.moveTo(pt1.x,pt1.y);
						g.lineTo(midpt.x,midpt.y);
					}
					prevMidpt = midpt;
				}
				g.lineTo(pt2.x,pt2.y);				
			}
		}
		public function set color(cl:uint):void{
			_color=cl;
		}
		public function get color():uint{
			return _color;
		}
//----------------------------------------------------------------------------------------------------PRIVATE--------------------------------													
													//================ Properties ===============
		private var container:Sprite;//由调用的容器传入，不必初始化；		
		private var _color:int;
		private var anList:Array;//存放变化的控制点；
		private var ptList:Array;//存放控制点的坐标；
		private var dragPt:Anchor;         //可被拖动的控制点object；
													//================ Private Methods ==========
		private function initSpr():void{
			container.addChild(this);
		}
		private function initVar():void{
			_color=0x66FFFF;//默认为黑色；
			anList=new Array();
		}
		private function initListener():void{
			container.addEventListener(MouseEvent.CLICK,clickHandler);
			container.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
		}
		private function clickHandler(evt:MouseEvent):void{
			if(evt.target is Anchor)return;
			var an:Anchor=new Anchor();
			an.x=mouseX;
			an.y=mouseY;
			anList.push(an);
			this.addChild(an);
			draw(); 
		}	
		private function downHandler(evt:MouseEvent):void{
			if(evt.target is Anchor){
				dragPt=Anchor(evt.target);
				container.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
				container.addEventListener(MouseEvent.MOUSE_UP,upHandler);
			}
		}	
		private function moveHandler(evt:MouseEvent):void{
			dragPt.x=mouseX;
			dragPt.y=mouseY;
			draw();
		}
		private function upHandler(evt:MouseEvent):void{
			dragPt=null;
			container.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			container.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		private function getPtList():void{
			ptList=new Array();
			var a:Anchor;
			for each (a in anList){
				var pt:Point=new Point(a.x,a.y);
				ptList.push(pt);
			}			
		}
	}
}

//包外类，绘制Anchor
	import flash.display.Sprite;
	import flash.display.Graphics;
	class Anchor extends Sprite{
		public function Anchor():void{
			init();
		}
		private function init():void{
			var gr:Graphics=this.graphics;
			gr.beginFill(0X454545,.1)
			gr.drawCircle(0,0,6);
			gr.lineStyle(0,0X454545,.6);
			gr.drawCircle(0,0,6);
			gr.beginFill(0X454545,.8);
			gr.drawCircle(0,0,4);
			gr.endFill();
		}
	}
	
	
	